package com.ymatou.iapi.sellercs.testcase;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.sellercs.parameter.IsLineBusyBean;
import com.ymatou.iapi.sellercs.parameter.SendMessageBean;
import com.ymatou.iapi.sellercs.service.IsLineBusyCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymatou.iapi.userservice.parameter.resp.UserInfo2;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.database.sqlwapper.YmtSellerCSIMWapper;
import com.ymttest.utils.EnvSetup;
/**
*是否占线
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_IsLineBusy{
	private static IsLineBusyBean islinebusyBean;
	private static IsLineBusyCall islinebusyCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static String sellername = EnvSetup.getData("autotest_sellerName1");
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	private static YmtSellerCSIMWapper YmtSellerCSIMWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("是否占线");
	}

	@Before
	public void caseUp() throws ClientProtocolException, IOException {
		islinebusyBean=new IsLineBusyBean();
		islinebusyCall=new IsLineBusyCall();
		YmtSellerCSIMWapper=new YmtSellerCSIMWapper();
		SellerCSIMCallService.OffLine(sellerId, null);
		SellerCSIMCallService.OffLine(zsellerId, null);
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(islinebusyCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_IsLineBusy_001() {
		Logger.start(true,"查询是否占线成功,用户无会话不占线");
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();

			islinebusyBean.setCSId(sellerId);
			islinebusyBean.setToUserId(userId);
			islinebusyCall.setData(islinebusyBean);
			islinebusyCall.callService();			
			Logger.verifyEquals("200", islinebusyCall.getString("Status"), "验证Status");
			Boolean IsBusy=islinebusyCall.getJsonObject().get("Result").getAsJsonObject().get("IsBusy").getAsBoolean();
//			Integer CsId=islinebusyCall.getJsonObject().get("Result").getAsJsonObject().get("CsId").getAsInt();
//			String CsName=islinebusyCall.getJsonObject().get("Result").getAsJsonObject().get("CsName").getAsString();
			Logger.verifyEquals(false, IsBusy, "验证IsBusy");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_IsLineBusy_002() {
		Logger.start(true,"查询是否占线成功,用户会话是当前用户占线");
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			//APP上线
			SellerCSIMCallService.AppLogin(sellerId);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, "connect");
			SellerCSIMCallService.SendMessage(sendmessageBean);
			//触发分配
			SellerCSIMCallService.Assign();
			Thread.sleep(3000);
			islinebusyBean.setCSId(sellerId);
			islinebusyBean.setToUserId(userId);
			islinebusyCall.setData(islinebusyBean);
			islinebusyCall.callService();			
			Logger.verifyEquals("200", islinebusyCall.getString("Status"), "验证Status");
			Boolean IsBusy=islinebusyCall.getJsonObject().get("Result").getAsJsonObject().get("IsBusy").getAsBoolean();
//			Integer CsId=islinebusyCall.getJsonObject().get("Result").getAsJsonObject().get("CsId").getAsInt();
//			String CsName=islinebusyCall.getJsonObject().get("Result").getAsJsonObject().get("CsName").getAsString();
			Logger.verifyEquals(false, IsBusy, "验证IsBusy");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_IsLineBusy_003() {
		Logger.start(true,"查询是否占线成功,用户有进行中会话且进行中会话的客服不是输入的CsId,IsBusy=true");
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			//APP上线
//			SellerCSIMCallService.AppLogin(sellerId);
			SellerCSIMCallService.AppLogin(sellerId);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, "connect");
			SellerCSIMCallService.SendMessage(sendmessageBean);
			//触发分配
			SellerCSIMCallService.Assign();
			Thread.sleep(3000);
			
			islinebusyBean.setCSId(zsellerId);
			islinebusyBean.setToUserId(userId);
			islinebusyCall.setData(islinebusyBean);
			islinebusyCall.callService();			
			Logger.verifyEquals("200", islinebusyCall.getString("Status"), "验证Status");
			Boolean IsBusy=islinebusyCall.getJsonObject().get("Result").getAsJsonObject().get("IsBusy").getAsBoolean();
			Integer CsId=islinebusyCall.getJsonObject().get("Result").getAsJsonObject().get("CsId").getAsInt();
			String CsName=islinebusyCall.getJsonObject().get("Result").getAsJsonObject().get("CsName").getAsString();
			Logger.verifyEquals(true, IsBusy, "验证IsBusy");
			UserInfo2 info = JUserCallService.GetUserInfo(sellerId);
			Logger.verifyEquals( sellerId,CsId, "验证CsId");
			Logger.verifyEquals( info.getLoginId(),CsName, "验证CsName");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}